<template>
  <div class="table-page">
    <GiTable
      row-key="userWechatGroupId"
      v-model:columns="columns"
      :loading="loading"
      :data="tableData"
      :scroll="{ x: '100%', y: '100%', minWidth: 1000 }"
      :pagination="pagination"
      @sorter-change="onSorterChange"
      @refresh="getTableData"
      :simple="true"
    >
      <template #custom-extra>
        <a-form
          label-align="right"
          layout="inline"
          auto-label-width
          :model="listParams"
          size="small"
          class="form my-10"
        >
          <a-form-item field="status" label="发送状态">
            <a-select
              v-model="listParams.status"
              @change="changeCurrent(1)"
              placeholder="请选择"
              class="w-30n"
              allow-clear
            >
              <a-option value="">全部</a-option>
              <a-option :value="0">进行中</a-option>
              <a-option :value="1">发群成功</a-option>
              <a-option :value="-1">发群失败</a-option>
            </a-select>
          </a-form-item>
          <a-form-item>
            <a-space>
              <a-button type="primary" size="small" @click="changeCurrent(1)">
                <template #icon>
                  <icon-search />
                </template>
                <template #default>查询</template>
              </a-button>
              <a-button size="small" @click="onResetSearch">
                <template #icon>
                  <icon-sync />
                </template>
                <template #default>重置</template>
              </a-button>
            </a-space>
          </a-form-item>
        </a-form>
      </template>
      <template #content="{ record }">
        <a-popover position="right">
          <div class="text-overflow-1">{{ record.content || '--' }}</div>
          <template #content>
            <div class="flex-col max-500">
              <div>{{ record.content }}</div>
            </div>
          </template>
        </a-popover>
      </template>
    </GiTable>
  </div>
</template>

<script setup>
import { usePagination } from '@/hooks'
import { oneClickGroupSendInfo } from '@/apis'
import { deleteEmpty } from '@/utils/common'
import GiDot from '@/components/GiDot/index.vue'

const props = defineProps(['userWechatId'])

const columns = ref([
  {
    title: '文案内容',
    tooltip: true,
    ellipsis: true,
    width: 300,
    slotName: 'content'
  },
  {
    title: '发送群',
    width: 100,
    render: (data) => {
      return data.record.groupName || '--'
    }
  },
  {
    title: '发群状态',
    width: 100,
    render: (data) => {
      const status = data.record.status
      const type = status == 1 ? 'success' : status == -1 ? 'danger' : 'info'
      const text = status == 1 ? '发送成功' : status == -1 ? '发送失败' : '发送中'
      return h(GiDot, {
        animation: false,
        type: type,
        text: text
      })
    }
  },
  {
    title: '出单量',
    width: 100,
    dataIndex: 'orderNum',
    sortable: {
      sortDirections: ['ascend', 'descend']
    },
    render: (data) => {
      const text = data.record.orderNum
      return text == 0 ? 0 : text || '--'
    }
  },
  {
    title: 'GMV',
    width: 100,
    dataIndex: 'gmv',
    sortable: {
      sortDirections: ['ascend', 'descend']
    },
    render: (data) => {
      const text = data.record.gmv
      return text == 0 ? 0 : text || '--'
    }
  },
  {
    title: '佣金',
    width: 100,
    dataIndex: 'commission',
    sortable: {
      sortDirections: ['ascend', 'descend']
    },
    render: (data) => {
      const text = data.record.commission
      return text == 0 ? 0 : text || '--'
    }
  },
  {
    title: '发群时间',
    width: 180,
    dataIndex: 'sendTime',
    sortable: {
      sortDirections: ['ascend', 'descend']
    },
    render: (data) => {
      return data.record.sendTime || '--'
    }
  }
])

const loading = ref(false)
const tableData = ref([])

const { pagination, setTotal } = usePagination(() => getTableData(), { defaultPageSize: 10 })

const listParams = reactive({
  page: 1,
  userWechatId: undefined,
  status: undefined
})

// 获取列表数据
const getTableData = async () => {
  try {
    loading.value = true
    listParams.page = pagination.current
    listParams.userWechatId = props.userWechatId
    const res = await oneClickGroupSendInfo(deleteEmpty(listParams))
    tableData.value = res.data.list
    setTotal(res?.data?.meta?.pagination?.total || 0)
  } finally {
    loading.value = false
  }
}

// 表格页码改变
const changeCurrent = (e) => {
  pagination.current = listParams.page = e
  getTableData()
}

watch(
  () => props.userWechatId,
  () => {
    changeCurrent(1)
  },
  { deep: true, immediate: true }
)

//升降序发生改变
const onSorterChange = (dataIndex, direction) => {
  listParams.orderBy = direction ? (direction === 'ascend' ? 'asc' : 'desc') : undefined
  listParams.sort = listParams.orderBy ? dataIndex : undefined
  changeCurrent(1)
}

// 列表重置搜索
const onResetSearch = () => {
  listParams.title = listParams.status = undefined
  changeCurrent(1)
}
</script>

<style lang="scss" scoped>
.table-page {
  height: 100%;
  overflow: hidden;
  background: var(--color-bg-1);
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  .form {
    :deep(.arco-form-item) {
      margin-bottom: 10px;
    }
  }
}
.max-500 {
  max-width: 500px;
}
</style>
